//
//  SettingViewController.m
//  RootDirectory
//
//  Created by Ryan on 13-2-28.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "SettingViewController.h"
#import "NYMemberCenterDataManager.h"
#import "NYVersionManager.h"
#import "NYWebViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

@synthesize settingTableView;
@synthesize settingProperties;
@synthesize aboutAppViewController;

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle:@"设置"];
    self.settingProperties = [NSArray arrayWithObjects:@"关于诺亚",@"关于App",/*@"推送通知",*/@"软件评分",@"更新提示", nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showFeedbackWebView:) name:kShowkFeedbackWebNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        self.view = nil;
        self.settingTableView = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.settingTableView = nil;
    self.settingProperties = nil;
    self.aboutAppViewController = nil;
    
}

- (void)dealloc
{
    [settingProperties release];
    [settingTableView release];
    [aboutAppViewController release];
    [super dealloc];
}


- (AboutAppViewController*)aboutAppViewController{
    if (aboutAppViewController == nil) {
        aboutAppViewController = [[AboutAppViewController alloc]init];
    }
    return aboutAppViewController;
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.settingProperties.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if(nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];
        
        UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_selection@2x.png"]];
        cell.selectedBackgroundView = bgView;
        [bgView release];
        
        cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon1_normal.png"]] autorelease];
        
        CGRect backImageRect = CGRectMake(10.0, 12.0, 20.0, 20.0);
        UIImageView *backImage = [[UIImageView alloc] initWithFrame:backImageRect];
        backImage.tag = 100;
        [cell.contentView addSubview:backImage];
        [backImage release];

        
        CGRect titleValueRect = CGRectMake(40.0, 2.0, 100.0, 40.0);
        UILabel *titleValue = [[UILabel alloc] initWithFrame:titleValueRect];
        titleValue.tag = 101;
        titleValue.backgroundColor = [UIColor clearColor];
        titleValue.textAlignment = UITextAlignmentLeft;
        titleValue.font = [UIFont systemFontOfSize:15];
        titleValue.adjustsFontSizeToFitWidth = YES;
        titleValue.minimumFontSize = 10;
        titleValue.textColor = [UIColor colorWithRed: 0.2 green: 0.2 blue: 0.2 alpha: 1.0];
        [cell.contentView addSubview:titleValue];
        [titleValue release];
    }
    
    
    NSInteger row = indexPath.row;
//    if(row == 1||row == 0||row == 3)
    if(row == 1||row == 0||row == 2)
        cell.accessoryView.hidden = NO;
    else
        cell.accessoryView.hidden = YES;
//    if (row == 2)
//    {
//        
//        NSString *statusNoti;
//        UIRemoteNotificationType status = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
//        if(status == UIRemoteNotificationTypeNone){
//            statusNoti = @"已关闭";
//        }
//        else{
//            statusNoti = @"已开启";
//        }
//        cell.detailTextLabel.text = statusNoti;
//      
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(250,10,60,30);
//        //[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
//        [button addTarget:self action:@selector(statusButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//        button.backgroundColor = [UIColor clearColor];
//        [cell addSubview:button];
//
//    }
    
    UIImageView *backImage = (UIImageView *)[cell.contentView viewWithTag:100];
    UILabel *titleValue = (UILabel *)[cell.contentView viewWithTag:101];

    titleValue.text = [self.settingProperties objectAtIndex:indexPath.row];
    switch (indexPath.row)
    {
        case 0:
            backImage.image = [UIImage imageNamed:@"icon14_normal@2x.png"];
            break;
        case 1:
            backImage.image = [UIImage imageNamed:@"icon12_normal@2x.png"];
            break;
        case 2:
            backImage.image = [UIImage imageNamed:@"icon15_normal@2x.png"];
            break;
        case 3:
            backImage.image = [UIImage imageNamed:@"icon13_normal@2x.png"];
        default:
            break;
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger row = indexPath.row;
    switch (row) {
        case 0:
        {
            NYWebViewController *nywvc = [[NYWebViewController alloc] init];
            nywvc.htmlType = kAboutUshtml;
            [self.navigationController pushViewController:nywvc animated:YES];
            [nywvc release];
        }
			break;
        case 1:
            //self.aboutAppViewController = [[[AboutAppViewController alloc]init]autorelease];
            [self.navigationController pushViewController:self.aboutAppViewController animated:YES];
            break;
//        case 2:
//            break;
        case 2:
            [[NSNotificationCenter defaultCenter]postNotificationName:kShowkFeedbackWebNotification object:nil];
            break;
        case 3:
        {
            [[NYVersionManager sharedManager] checkVersion];
        }
            break;
        default:
            break;
    }
}

- (void)showFeedbackWebView:(NSNotification *) aNotification
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:kAppRateUrl,kAppAppleId]]];
}

#pragma mark -
- (void)switchChanged:(NSNotification *) aNotification{
    NSLog(@"switch change!");
}

- (void)statusButtonPressed:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"设置推送通知"
                                                    message:@"请在iphone的”设置“-”通知“功能中，找到应用程序”诺亚“进行更改"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
@end
